using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
namespace etbTOEIC
{
	/// <summary>
	/// Summary description for FormBuildReport.
	/// </summary>
	public class FormBuildReport : System.Windows.Forms.Form
	{
		private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
	//	private etbReport rp;
			private MainReport rp;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FormBuildReport()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormBuildReport));
			this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
			this.SuspendLayout();
			// 
			// crystalReportViewer1
			// 
			this.crystalReportViewer1.ActiveViewIndex = -1;
			this.crystalReportViewer1.DisplayGroupTree = false;
			this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0);
			this.crystalReportViewer1.Name = "crystalReportViewer1";
			this.crystalReportViewer1.ReportSource = null;
			this.crystalReportViewer1.Size = new System.Drawing.Size(784, 350);
			this.crystalReportViewer1.TabIndex = 0;
			// 
			// FormBuildReport
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(784, 350);
			this.Controls.Add(this.crystalReportViewer1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "FormBuildReport";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Build Report";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.FormBuildReport_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void FormBuildReport_Load(object sender, System.EventArgs e)
		{
			rp = new MainReport();
			
			if(DB2MainForm.isDisplayClass)
				rp.SetParameterValue("Class","Class   : " + DB2MainForm.stClass);
			else
				rp.SetParameterValue("Class","");
			
			if(DB2MainForm.isDisplaySchool)
				rp.SetParameterValue("School","School : " + DB2MainForm.stSchool);
			else
				rp.SetParameterValue("School","");

			if(DB2MainForm.isDisplayExamCode)
				rp.SetParameterValue("ExamCode","ExamCode : " + DB2MainForm.ExamCode);
			else
				rp.SetParameterValue("ExamCode","");

			rp.SetParameterValue("Direction",DB2MainForm.stPageDirection);
			rp.SetParameterValue("Header", DB2MainForm.stReportHeader);
			rp.SetParameterValue("Footer", DB2MainForm.stReportFooter);
			
			foreach (Section section in rp.ReportDefinition.Sections)
			{
				// In each section we need to loop through all the reporting objects
				foreach (ReportObject reportObject in section.ReportObjects)
				{
					if (reportObject.Kind == ReportObjectKind.SubreportObject)
					{
						SubreportObject subReport = (SubreportObject)reportObject;
						ReportDocument  subDocument = subReport.OpenSubreport(subReport.SubreportName);

						foreach (Table table in subDocument.Database.Tables)
						{
							// Cache the logon info block
							TableLogOnInfo logOnInfo = table.LogOnInfo;

							// Set the connection
							logOnInfo.ConnectionInfo.ServerName = Application.StartupPath + "\\dbTOEFL.mdb";

							// Apply the connection to the table!
							table.ApplyLogOnInfo(logOnInfo);
						}
					}
				}
			}

			
			Part1();
			Part6();
			Part7();
			crystalReportViewer1.ReportSource = rp;
		}
		void Part1()
		{
			DataSet ds = new DsPart1();
			MyDatabase.GetData4Part1(ref ds);
		
			foreach(DataRow row in ds.Tables[0].Rows)
			{				
				string FileName = row.ItemArray[2].ToString();
				FileName = Environment.CurrentDirectory + "\\Images\\" + FileName;
				//insert image
				try
				{			
					row["image"] = ReadImage(FileName);
				}
				catch(Exception er)
				{
					MessageBox.Show(er.Message,"Error");
				}
			}
			
			//create the report object
			rp.OpenSubreport("rpPart1.rpt").SetDataSource(ds);
			
				
		}
		void Part6()
		{
			
			DataSet ds = new DsPart6();
			MyDatabase.GetData4Part6(ref ds);
			foreach(DataRow row in ds.Tables[0].Rows)
			{
				string stQues = row.ItemArray[2].ToString();
				stQues = stQues.Replace("&nbsp;", " ");
				stQues = stQues.Replace("<br>", @"\par\r\n");
				stQues = stQues.Replace("<U>", @"\ul ");
				stQues = stQues.Replace("</U>", @"\ulnone ");
				stQues = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}\r\n\viewkind4\uc1\pard\f0\fs24" + stQues ;
				stQues += @"\par\r\n\pard\f1\fs17\par\r\n}";
				row["QuestionText"] = stQues;
			}
			
			rp.OpenSubreport("rpPart6.rpt").SetDataSource(ds);

		}

		void Part7()
		{
			DataSet ds = new DsPart7();

			MyDatabase.GetData4Part7(ref ds);
			
		
			foreach(DataRow row in ds.Tables[0].Rows)
			{
				
				string FileName = row.ItemArray[8].ToString();
				if (FileName!="")
				{
					FileName = Environment.CurrentDirectory + "\\Images\\" + FileName;
				
					//insert image
					try
					{
						row["image"] = ReadImage(FileName);
						
					}
					catch(Exception er)
					{
						MessageBox.Show(er.Message,"Error");
					}
				}
			}
		
			rp.OpenSubreport("rpPart7.rpt").SetDataSource(ds);
			
		}
		public byte[] ReadImage(string stImagePath)
		{
			FileStream FilStr = new FileStream(stImagePath, FileMode.Open);
			BinaryReader BinRed = new BinaryReader(FilStr);
			byte[] res = BinRed.ReadBytes((int)BinRed.BaseStream.Length);
			FilStr.Close();
			BinRed.Close();
			return res;
		}
	

	}
}
